package cn.zspt.vank.controller;

import cn.zspt.vank.domain.Dish;
import cn.zspt.vank.domain.User;
import cn.zspt.vank.domain.dto.DishSale;
import cn.zspt.vank.service.AdminService;
import cn.zspt.vank.util.MD5Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "管理员api",description = "管理员功能api")
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @GetMapping("/statics")
    @Operation(summary = "统计各项数据", description = "此接口处理的年月日销售金额")
    public Map<String,Object> getStatics(){
        return adminService.getStatics();
    }

    @GetMapping("/dishSaleSort/{sortType}")
    @Operation(summary = "菜品销量排序")
    public List<DishSale> getDishSaleSort(
            @Parameter(description = "排序类型，分别是day,month,year", required = true, example = "day")
            @PathVariable String sortType){
        //System.out.println(sortType);
        return adminService.getDishSaleSort(sortType);
    }

    @GetMapping("/user")
    @Operation(summary = "获得所有用户数据",description = "不含密码")
    public List<User> getAllUser(){
        return adminService.findAllUser();
    }

    @PutMapping("/user/{userId}")
    @Operation(summary = "切换用户状态",description = "启用-不启用之间切换")
    public void switchUserState(@PathVariable Integer userId){
        adminService.switchUserState(userId);
    }

    @PostMapping("/user")
    @Operation(summary = "新增用户",description = "用户初始密码123")
    public void saveUser(@RequestBody User user){
        user.setPassword(MD5Utils.generateEncryp("123"));
        adminService.saveUser(user);
    }

    @GetMapping("/dish")
    @Operation(summary = "获取所有菜品信息")
    public List<Dish> getAllDish(){
        return adminService.findAllDish();
    }

    @PutMapping("/dish")
    @Operation(summary = "修改菜品信息")
    public void updateDish(@RequestBody Dish dish){
        adminService.updateDish(dish);
    }

    @PostMapping("/dish")
    @Operation(summary = "新增菜品")
    public void saveDish(@RequestBody Dish dish){
        adminService.saveDish(dish);
    }

    @Value("${image.dishes_path}")
    private String dishImgPath;
    @PostMapping(path = "/uploadDishImage",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传菜品图片")
    public String uploadDishImage(
            @Parameter(description = "要上传的图片文件", required = true)
            MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        int i = filename.lastIndexOf(".");
        String ext = filename.substring(i);

        String imgName = (new Date()).getTime() + ext;
        File filepath = new File(dishImgPath, imgName);
        //判断路径是否存在，如果不存在就创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
        //将上传文件保存到一个目标文件当中
        file.transferTo(new File(dishImgPath + imgName));
        return "dishes_img/" + imgName;
    }
}
